<script setup>
import { onMounted, ref } from 'vue'

import { useTableList } from '@/hooks/useTableList'
import {
  fetchCarGateDeviceList,
  deleteCarGateDevice,
} from '@/api/manager/dataManage/deviceManage.js'
import CarGatesAdd from './carGatesAdd.vue'
import CarGatesDetail from './carGatesDetail.vue'
import CarPlateList from './carPlateList.vue'

defineOptions({
  name: 'CarGates',
})

const props = defineProps({
  currentType: {
    type: String,
    default: '',
  },
  parkList: {
    type: Array,
    default: [],
  },
})

const showPlate = ref(false)

const carGateId = ref('')

const {
  toggleSearchStatus,
  tableData,
  paginationParams,
  total,
  initTableList,
  handleSizeChange,
  addRef,
  detailRef,
  currentDetail,
  handleAdd,
  handleEdit,
  handleDetail,
  handleDelete,
  handleClose,
  searchFromRef,
} = useTableList({}, fetchCarGateDeviceList, deleteCarGateDevice)

const handleItemClick = (data) => {
  carGateId.value = data.pkid
  showPlate.value = true
}

const onBackFromPlateList = () => {
  showPlate.value = false
  carGateId.value = ''
}

const handleAddDialogClose = (shouldRefresh) => {
  handleClose(shouldRefresh)
}

onMounted(() => {
  initTableList(1)
})
</script>

<template>
  <div
    v-show="!showPlate"
    class="search search1"
    :class="{ active: toggleSearchStatus }"
  >
    <div class="search-left" ref="searchFromRef"></div>
    <div class="search-right">
      <el-button type="primary" @click="initTableList(1)"
        ><svg-icon name="search" size="14px" />搜索</el-button
      >
      <!-- <el-button @click="handleReset(null)"
        ><svg-icon name="reset" size="17px" />重置</el-button
      > -->
    </div>
  </div>
  <div v-show="!showPlate" class="content">
    <div class="content-operate">
      <el-button type="primary" @click="handleAdd('新增闸机')">
        <svg-icon name="add" size="15px" />
        新增闸机
      </el-button>
    </div>

    <div class="content-table">
      <el-table
        class="custom-table"
        :data="tableData"
        show-overflow-tooltip
        :tooltip-options="{ showAfter: 300 }"
      >
        <el-table-column type="index" label="序号" width="60px" />
        <el-table-column prop="deviceName" label="设备名称">
          <template #default="scope">
            <el-link
              type="success"
              underline
              @click="handleItemClick(scope.row)"
              >{{ scope.row.deviceName }}</el-link
            >
          </template>
        </el-table-column>
        <el-table-column prop="nickName" label="设备昵称" />
        <el-table-column prop="ip" label="ip地址" />
        <el-table-column prop="port" label="端口" />
        <el-table-column prop="serialNumber" label="序列号" />
        <el-table-column prop="deviceType" label="设备类型 " />
        <el-table-column prop="model" label="设备型号 " />
        <el-table-column label="操作" fixed="right" width="160px">
          <template #default="scope">
            <el-button
              link
              type="primary"
              @click="handleEdit(scope.row, '编辑闸机')"
              >编辑</el-button
            >
            <el-button link type="primary" @click="handleDetail(scope.row)"
              >详情</el-button
            >
            <el-button link type="danger" @click="handleDelete(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="content-page">
      <el-pagination
        class="custom-pagination"
        :current-page="paginationParams.pageIndex"
        :page-size="paginationParams.pageSize"
        background
        :page-sizes="[10, 20, 30, 50]"
        layout="total, prev, pager, next, sizes, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="initTableList"
      />
    </div>
  </div>

  <CarPlateList
    v-if="showPlate"
    :gate-id="carGateId"
    :current-type="currentType"
    :park-list="parkList"
    @back="onBackFromPlateList"
  />

  <CarGatesAdd
    ref="addRef"
    :detail="currentDetail"
    :park-list="parkList"
    @close="handleAddDialogClose"
  />
  <CarGatesDetail
    ref="detailRef"
    :detail="currentDetail"
    :park-list="parkList"
  />
</template>
